package com.swak.boot.config.jdbc;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import com.swak.boot.config.jdbc.database.DataSourceProperties;
import com.swak.boot.config.jdbc.database.DruidDataSourceAutoConfiguration;
import com.swak.boot.config.jdbc.database.HikariDataSourceAutoConfiguration;
import com.swak.boot.config.jdbc.database.SqlLiteDataSourceAutoConfiguration;
import com.swak.boot.config.jdbc.sharding.ShardingJdbcConfiguration;
import com.swak.persistence.datasource.DataSourceHolder;
import com.swak.persistence.dialect.Dialect;

/**
 * 配置方言
 * 
 * @author lifeng
 * @date 2021年2月2日 下午1:04:50
 */
@Configuration
@ConditionalOnClass(JdbcTemplate.class)
@ConditionalOnSingleCandidate(DataSource.class)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, ShardingJdbcConfiguration.class,
		SqlLiteDataSourceAutoConfiguration.class, DruidDataSourceAutoConfiguration.class,
		HikariDataSourceAutoConfiguration.class })
public class DialectAutoConfiguration {

	@Bean
	@Primary
	@ConditionalOnMissingBean(Dialect.class)
	@ConditionalOnSingleCandidate(DataSourceProperties.class)
	public Dialect dbDialect(DataSourceProperties dbProperties) {
		Dialect dialect = Dialect.getDialect(dbProperties.getUrl());
		DataSourceHolder.setDialect(dialect);
		return dialect;
	}
}
